import mapInit from "./mapInit";

const {ccclass, property} = cc._decorator;

@ccclass
export default class newMapControl extends cc.Component {

    //  获取瓦片图资源
    map: cc.TiledMap;
    player: cc.Node = null;
    
    onLoad () {
        // getPhysicsManager物理管理器
        let gp:cc.PhysicsManager = cc.director.getPhysicsManager();
        gp.enabled = true;
        // 绘制调试区域
        var Bits = cc.PhysicsManager.DrawBits;
        // gp.debugDrawFlags = Bits.e_shapeBit;
    }

    start () {
        this.map = this.getComponent(cc.TiledMap);
        // 生成阻挡层刚体
        mapInit('hinder', 0, this.map);

        // 获取指定的对象层
        let playerObj = this.map.getObjectGroup('playObj');
        
        // 获取对象
        let playerStartPos = playerObj.node.getChildByName("startPos");

        // 在开始位置创建玩家对象
        // 加载资源
        cc.resources.load('prefab/tilemap/player', cc.Prefab,  (err, playerPre)=> { 
            // 实例化预设体
            this.player = cc.instantiate(playerPre);
            this.player.setParent(this.node.getChildByName('playObj'));
            
            // // // 设置玩家位置
            this.player.x= playerStartPos.x;
            this.player.y = playerStartPos.y;
        })
    }

    update (dt) {
        // 摄像头跟随玩家
        if (this.player != null) {
            cc.Camera.main.node.x = this.player.x;
            cc.Camera.main.node.y = this.player.y;
        }
    }
}
